package com.mobiusws.reports.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import com.mobiusws.reports.action.DailyFraudDetailAction;
import com.mobiusws.reports.action.DailyFraudSummaryAction;
import com.mobiusws.reports.action.WeeklyFraudSummaryAction;
import com.mobiusws.reports.model.CallType;
import com.mobiusws.reports.model.DailyFraudSummary;
import com.mobiusws.reports.model.LoginInfo;
import com.mobiusws.util.JsonUtil;
import com.mobiusws.util.SqlUtil;

@Repository("weeklyFraudSummaryDao")
public class WeeklyFraudSummaryDao {
	@Resource
	private JdbcTemplate dbxJdbc;
	@Resource
	private JdbcTemplate coreJdbc;
	@Resource
	private SqlUtil sqlUtil;

	
	public List<DailyFraudSummary> getWeeklyFraudSummary(WeeklyFraudSummaryAction action) {
		if(action.getTotal()<1) {
			String sqlCnt=sqlUtil.getSqlById("getWeeklyFraudSummaryCount");
			int cnt=dbxJdbc.queryForInt(sqlCnt, new Object[] {action.getStartDate(), action.getEndDate()});
			action.setTotal(cnt);
		}
		
		String sql=sqlUtil.getSqlById("getWeeklyFraudSummary");
		List<DailyFraudSummary> list = dbxJdbc.query(sql, new Object[] {action.getStartDate(), action.getEndDate(), action.getStart(), action.getLimit()},
				new BeanPropertyRowMapper(DailyFraudSummary.class));
		return list;
	}
	
	public List<List<Object>> getQueryList(WeeklyFraudSummaryAction action) {
		List<List<Object>> ls = new ArrayList<List<Object>>();
		try {
			Connection conn = dbxJdbc.getDataSource().getConnection();
			ResultSet rs = null;
			List<Object> lsl = null;
			String sql = sqlUtil.getSqlById("getWeeklyFraudSummaryExport");
			java.sql.PreparedStatement stmt = null;
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, action.getStartDate());
			stmt.setString(2, action.getEndDate());
			rs = stmt.executeQuery();
			int column = rs.getMetaData().getColumnCount();
			while(rs.next()) {
				lsl = new ArrayList<Object>();
				for(int i=0; i < column; i++) {
					lsl.add(rs.getString(i+1));
				}
				ls.add(lsl);
			}
			
			stmt = null;
			rs = null;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		
		return ls;
	}

}
